<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2017/12/29
 * Time: 18:31
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\Standard;
use App\Http\Logics\BaseLogic;
class StandardLogic extends BaseLogic
{
    /**
     * 添加规格
     * @param $data 规格信息
     * @return mixed 返回添加结果
     */
    public static function createStandard($data)
    {
        try {
            $standard = new Standard();
            $standard->name = $data['name'];
            if (!$standard->save()) {
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function getStandardList($data)
    {
        $standard = Standard::where(function ($query) use ($data){
            if (isset($data['status']) && $data['status'] != ''){
                $query->where('status', $data['status']);
            }
        })->orderBy('sort')->get();
        if (empty($standard)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $standard->map(function ($standard) {
            return $standard->append('status_bool');
        });
        return self::response(self::SUCCESS, $standard);
    }

    public static function deleteStandard($data)
    {
        $standard = Standard::find($data['standard_id']);
        if (empty($standard)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$standard->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }

    public static function updateStandard($data)
    {
        $standard = Standard::find($data['standard_id']);
        if (empty($standard)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $standard->name = $data['name'];
        if (!$standard->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function setSort($data)
    {
        $new_sort = explode(',', $data['new_sort_str']);

        foreach ($new_sort as $key => $item){
            $standard = Standard::find($item);
            if (empty($standard)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $standard->sort = $key;
            if (!$standard->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
        }
        return self::response(self::SUCCESS);
    }

    public static function changeStatus($data)
    {
        try{
            $standard = Standard::find($data['standard_id']);
            if (empty($standard)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $standard->status = !$standard->status;
            if (!$standard->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }
}